import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:provider/provider.dart';
import 'package:wan_android_flutter/pages/knowledge/detail/knowledge_detail_tab_page.dart';
import 'package:wan_android_flutter/pages/knowledge/knowledge_vm.dart';
import 'package:wan_android_flutter/route/RouteUtils.dart';
import 'package:wan_android_flutter/route/routes.dart';

import '../../repository/datas/knowledge_list_data.dart';

class KnowledgePage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _KnowledgePageState();
  }
}

class _KnowledgePageState extends State<KnowledgePage> {
  KnowledgeViewModel viewModel = KnowledgeViewModel();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    viewModel.knowledgeList();
  }

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (context) {
        return viewModel;
      },
      child: Scaffold(
        body: SafeArea(
          child: Consumer<KnowledgeViewModel>(
            builder: (context, vm, child) {
              return ListView.builder(
                itemCount: vm.list.length,
                itemBuilder: (context, index) {
                  return _itemView(vm.list[index]);
                },
              );
            },
          ),
        ),
      ),
    );
  }

  Widget _itemView(KnowledgeListData? item) {
    return GestureDetector(
      onTap: () {
        //进入明细页面
        RouteUtils.push(context, knowledgeDetailTabPage(item?.children));
      },
      child: Container(
          decoration: BoxDecoration(
              border: Border.all(color: Colors.black12), //边框颜色
              borderRadius: BorderRadius.circular(5.r)), //边框圆角
          margin: EdgeInsets.symmetric(horizontal: 5, vertical: 10),
          padding:
              EdgeInsets.only(left: 15.w, right: 15.w, top: 10.h, bottom: 10.h),
          child: Row(
            children: [
              Expanded(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                      item?.name ?? "",
                      style: TextStyle(
                          fontSize: 16.sp, fontWeight: FontWeight.bold),
                    ),
                    SizedBox(height: 5.h),
                    Text(
                      viewModel.generalSubTitle(item?.children),
                      style:
                          TextStyle(fontSize: 14.sp, color: Colors.grey[700]),
                    ),
                  ],
                ),
              ),
              Image.asset(
                "assets/images/img_arrow_right.png",
                width: 15.w,
                height: 20.h,
              )
            ],
          )),
    );
  }
}
